<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>搜索</title>
    <link rel="stylesheet" href="css/animate.css"/>
    <link rel="stylesheet" href="css/font-awesome.min.css"/>
    <link rel="stylesheet" href="css/flexslider.css"/>
    <link rel="stylesheet" href="css/base.css?20150803"/>
    <style type="text/css">
        .ms-controller, [ms-controller] {
            display: none;
        }
    </style>
</head>
<body>

<!-- header 顶部 -->
<header>
     <a href="index.html" style="padding-left:10px;"><img src="images/e-jialogo.png" alt="" style="width:500px;height:70px;" /></a>
    <span class="right">
        <input type="text" class="headInput" id="keyinput" placeholder="输入商品名称"/><input type="button" class="headBtn" id="btnsearch" value="搜索"/>
    </span>
</header>


<div class="road">
    <a href="javascript:history.back(-1)"><i class="fa fa-angle-left"></i></a>
    <span id="r_cat1">搜索结果</span>
    <hr/>
</div>

<div class="fliterWrap">
    <div class="panel">
        <div class="panel-heading">
            共显示<b id="proqty">0</b>款相关产品
        </div>
    </div>

    <div class="sort">
        <a href="javascript:;" class="active">价格从低到高</a>
        <a href="javascript:;">价格从高到低</a>
    </div>

</div>


<!-- 商品列表 -->
<div class="goodList">
    <ul>
        
    </ul>
</div>


<script src="js/jquery.min.js"></script>
<script src="js/avalon.mobile.min.js"></script>
<script src="js/MobileCom.js"></script>
<script>

    var loc = window.localStorage.getItem("loc");
    var keysr = MobileCom.getQueryString("key");

    
    $(function () {
        if (window.localStorage.getItem("EUser") == null) {
            window.location = "login.html";
        }

        if (keysr) {
            $("#keyinput").val(keysr);
        }
        window.localStorage.removeItem("page");
        window.localStorage.setItem("page", 1);


        $(".sort a").click(function(){
            $(this).siblings("a").removeClass("active").end().addClass("active");
            window.localStorage.removeItem("page");
            window.localStorage.setItem("page", 1);

            loadMore("1");

        });

        $("#btnsearch").click(function () {
            window.location = "search.html?key=" + $("#keyinput").val();
        })
        $("#keyinput").keydown(function (e) {

            if (e.keyCode == 13) {
                window.location = "search.html?key=" + $("#keyinput").val();
            }
        })
        
        loadMore("1");
    });


    function loadMore(flag) {
        var page = parseInt(window.localStorage.getItem("page"));
        if (page > 0) {

            var clist = "";

            var st = $(".sort a").index($(".sort a.active"));
            if (st == "0") {
                st = "1";
            } else if (st == "1") {
                st="2";
            } else {
                st = "1";
            }


            var vcat = {
                "key": keysr,
                "sort": st
            };

            var url = "/View/SpeList/DLLAjaxHandler.ashx?action=SearchProcute";
            if (loc != null && loc == 2) {
                url += "&euser=" + window.localStorage.getItem("EUser");
            }

            $.getJSON(url, vcat, function (r) {

                if (r.length>0) {
                    $("#proqty").text(r.length);

                    var imgli = "";
                    $(r).each(function (index, item) {
                        imgli += " <li class=\"goodItem\"><a href=\"shopDetail.html?id=" + item.ID + "\"><img src=\"" + item.ImgUrl1 + "\" /><p><span>" + item.Title + "</span><b>￥" + item.Price2 + "</b><del>原价：￥" + item.Price1 + "</del></p></a></li>";
                    })

                    if (flag) {
                        $(".goodList ul").html(imgli);
                    } else {
                        $(".goodList ul").append(imgli);
                    }
                   

                    var nxt = page + 1;
                    window.localStorage.setItem("page", nxt);
                } else {
                    if (flag) {
                        $(".goodList ul").html("");
                    }
                    window.localStorage.setItem("page", 0);
                }
            })

        }

    }
</script>
</body>
</html>